(function ($) {
  "use strict";
  /*
  * author GL
  * params = {
  *   input 当前绑定事件的节点 String
  *   title 标题 String
  *   linkage 联动 Array 详见demo
  * }
  * */
  var defaults = {
    target: 'input',
    title: "标题",
    linkage: [],
    callback: function () {}
  };
  $.fn.linkage = function (option) {
    var self = this;
    option = $.extend({},option);
    option.target = self;
    self.prop("readonly", true);
    self.on("click", function () {
      $.linkage(option);
    });
  };
  $.linkage = function (option) {
    var params = $.extend({},defaults,option);
    var tpl = '<div class="gzl-select-popup"><div class="gzl-select__mask"></div>' +
      '<div class="gzl-select__box">' +
      '<div class="gzl-select__hd">' +
      '<div class="gzl-select__close"><svg class="icon"><use xlink:href="#icon-close"></use></svg></div>' +
      '<div class="gzl-select__tit"><strong>'+params.title+'</strong></div>' +
      '<div class="gzl-select__nav"></div>' +
      '</div>' +
      '<div class="gzl-select__bd">' +
      '</div>' +
      '</div>' +
      '</div>';
    var select = $(tpl).appendTo(document.body);
    $.noScroll();
    $(".gzl-select__mask, .gzl-select__box", select).show().addClass("show");
    var curVal = $(params.target).data("val");
    if (curVal) {
      curVal = curVal.split('/');
      if(curVal.length > 1){
        for (var i=0;i<curVal.length;i++) {
          if (i < curVal.length-1) {
            loadData(params.linkage);
            $("input[data-val='"+curVal[i]+"']").parent().click();
          }else{
            $("input[data-val='"+curVal[i]+"']").prop("checked",true);
          }
        }
      }else {
        loadData(params.linkage);
        $("input[data-val='"+curVal[0]+"']").prop("checked",true);
      }
    }else{
      loadData(params.linkage);
    }
    function loadData(arr){
      var sel = $('<ul class="gzl-select__list gzl-cell_check"></ul>').appendTo(".gzl-select__bd", select);
      var sindex = sel.index();
      $(".gzl-select__nav", select).append('<span class="active">请选择</span>');
      for(var i=0;i<arr.length;i++){
        sel.append('<li class="gzl-select__option"><input class="gzl-check" data-val="'+arr[i].name+'" name="select'+sindex+'" type="radio"><em class="gzl-check__info">'+(arr[i].con?arr[i].con:arr[i].name)+'</em><svg class="icon gzl-check__icon"><use xlink:href="#icon-tick"></use></svg></li>')
      }
      $(".gzl-select__option", sel).on("click", function () {
        var sels = $(this).parents().find(".gzl-select__list");
        var nav = $(".gzl-select__nav span", select);
        for(var i=sels.length-1;sels[i] !== $(this).parent()[0];i--){
          sels[i].remove();
          nav[i].remove();
        }
        var index = $(this).index();
        var cate=arr[index];
        if(cate.sub){
          $(this).find("input").prop("checked", true);
          nav.eq(sindex).removeClass("active").text($(this).find("input").data("val"));
          loadData(cate.sub);
          $(this).parent().hide().next().fadeIn(200);
        }else{
          curVal = '';
          nav.eq(sindex).text('请选择');
          $(this).find("input").prop("checked", true);
          $("input:checked", select).each(function (i) {
            curVal += (i!==0?'/':'') + $(this).data("val");
          });
          $(params.target).html(curVal).data("val", curVal).removeClass("placeholder");
          params.callback(curVal, params.target);
          $.hideLinkage();
        }
      })
    }
    $(".gzl-select__nav").on("click", "span", function () {
      $(this).addClass("active").siblings().removeClass("active");
      $(".gzl-select__list", select).hide().eq($(this).index()).fadeIn(200);
    });
    // 隐藏
    $(".gzl-select__close, .gzl-select__mask", select).on("click", function () {
      $.hideLinkage();
    });
  };
  $.hideLinkage = function () {
    $(".gzl-select-popup").find(".gzl-select__mask, .gzl-select__box").removeClass("show").transitionEnd(function () {
      $(".gzl-select-popup").remove();
      $.hasScroll();
    })
  };
})($);
